<?php
/* 
 * Copyright (C) 2012 Leah Rowe <info@minifree.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * This checks each server in the database and emails all admins if a server
 * goes down or if an offline server comes back online.
 * In addition to email, this will create a log entry in the database if any
 * event is found.
 */

include "common.php";

function serverCheck()
{
	$qstat = qstatArray();
	$server = serverArray();
	
	for ($i = 0; $i < count($server["full"]); $i++)
	{
		$serverfound = false;
	
		for ($j = 0; $j < count($qstat["content"]); $j++)
		{
			$serverfound |= strpos($qstat["content"][$j],
				$server["full"][$i]) !== false;
		}
		
		if ($serverfound && $server["serverdown"][$i] == "1")
			/* 
			 * Server was listed as down (serverdown='1'), but is
			 * now back online, so update serverdown='0', then
			 * record a log and send an email to all admins
			 */
			inject("UPDATE servers SET serverdown='0' WHERE ".
				"(ipaddr = '".$server["ipaddr"][$i]."' AND ".
				"portnum = '".$server["portnum"][$i]."')");
			
			record("Server '".$server["full"][$i]."' back online,".
				" having previously been offline",
				"oacron.php");
			compose("Server '".$server["full"][$i]."' back online",
				"Please check log entries.\n\nGenerated by ".
				"oacron.php");
		} 
		// server not found on any specified QStat v2.11 lists 
		else if ($server["serverdown"][$i] == "0")
			/*
			 * Server was listed as online (serverdown='0'), but
			 * is now offline, so update serverdown='1', then
			 * record a log and send an email to all admins
			 */
			inject("UPDATE servers SET serverdown='1' WHERE ".
				"(ipaddr = '".$server["ipaddr"][$i]."' AND ".
				"portnum = '".$server["portnum"][$i]."')");
			
			record("Server '".$server["full"][$i]."' has gone ".
				"offline, having previously been online",
				"oacron.php");
			compose("Server '".$server["full"][$i]."' has gone ".
				"offline","Please check log entries.".
				"\n\nGenerated by oacron.php");
		}
	}
	
	return "server check was performed";
}

serverCheck();
?>
